package cn.jlonely.xyhx.app.mapper;

import cn.jlonely.xyhx.pojo.User;
import cn.jlonely.xyhx.pojo.vo.AppUserVo;
import cn.jlonely.xyhx.pojo.vo.UserVo;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UseMapper {

    /**
     * 根据用户名查询用户是否存在
     *
     * @param username
     * @return
     */
    @Select("SELECT COUNT(*) FROM users WHERE username = #{username}")
    int countByUsername(String username);

    default boolean selectByUsername(String username) {
        return countByUsername(username) == 0; // 记录数为 0，说明用户不存在
    }
    /**
     * 用户登录
     * @param userVo
     * @return
     */
    @Select("select * from users where username=#{username} and password=#{password}")
    @Results({
            @Result(property = "id", column = "user_id")
    })
    User selectByUsernameAndPassword(UserVo userVo);

    /**
     * 用户注册
     * @param user
     */
    @Insert("insert into users(username, nickname, real_name,password, college, grade, user_class, contact_info, avatar_url, sex) values(#{username}, #{nickname}, #{realName},#{password} ,#{college}, #{grade}, #{userClass}, #{contactInfo}, #{avatarUrl}, #{sex})")
    void register(AppUserVo user);

    /**
     * 更新用户信息
     * @param appUserVo
     */
    void update(AppUserVo appUserVo);
}
